/*
 * $Id: CompareWithHeadAction.java 23 2002-09-29 13:59:30Z bpruessmann $
 * 
 * Copyright (c) 2001,2002 Boris Pruessmann and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v0.5
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors:
 *    Boris Pruessmann - Initial implementation.
 */
package net.sourceforge.p4eclipse.ui.action;

import java.lang.reflect.InvocationTargetException;

import net.sourceforge.p4eclipse.core.PerforceRepositoryProvider;
import net.sourceforge.p4eclipse.core.resources.RemoteResource;
import net.sourceforge.p4eclipse.ui.PerforceCompareEditorInput;
import net.sourceforge.p4eclipse.ui.PerforceResourceEditionNode;
import net.sourceforge.p4eclipse.ui.PerforceResourceNode;
import net.sourceforge.p4eclipse.ui.PerforceUIPlugin;
import org.eclipse.compare.CompareUI;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.team.core.RepositoryProvider;
import org.eclipse.team.core.TeamException;
import org.eclipse.team.internal.ui.actions.TeamAction;

/**
  * @version 	$Revision: 23 $
  * @author		<a href="mailto:bpruessmann@users.sourceforge.net">Boris Pruessmann</a>
  */
public class CompareWithHeadAction extends TeamAction
{
	/*
	 * @see IActionDelegate#run(IAction)
	 */
	public void run(IAction action)
	{
		run(
			new IRunnableWithProgress() 
			{
				public void run(IProgressMonitor monitor) 
					throws InvocationTargetException 
				{
					try 
					{
						IResource[] resources = getSelectedResources();
						if (resources.length != 1) 
							return;
							
						PerforceRepositoryProvider provider = (PerforceRepositoryProvider)RepositoryProvider.getProvider(resources[0].getProject());
						
						RemoteResource remoteResource = (RemoteResource)provider.getRemoteTree(resources[0], "#head", new NullProgressMonitor());
						RemoteResource ancestorResource = (RemoteResource)provider.getRemoteTree(resources[0], "#have", new NullProgressMonitor());

						CompareUI.openCompareEditor(new PerforceCompareEditorInput(new PerforceResourceNode(resources[0]), new PerforceResourceEditionNode(remoteResource), new PerforceResourceEditionNode(ancestorResource)));
					} 
					catch (TeamException e) 
					{
						throw new InvocationTargetException(e);
					}
				}
		}, "CompareWithHeadAction.compare", PROGRESS_BUSYCURSOR);		
	}


	/*
	 * @see TeamAction#isEnabled()
	 */
	protected boolean isEnabled() 
		throws TeamException
	{
		IResource[] resources = getSelectedResources();
		if (resources.length != 1) 
			return false;
			
		RepositoryProvider tempProvider = RepositoryProvider.getProvider(resources[0].getProject());
		if (tempProvider == null || !tempProvider.getID().equals(PerforceUIPlugin.getTypeId()))
			return false;
				
		PerforceRepositoryProvider provider = (PerforceRepositoryProvider)tempProvider;
		if (!(provider.hasRemote(resources[0])))
		{
			return false;
		}
				
		return true;
	}
}
